
package aula_serializacao;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map.Entry;

public class CadastroAluno {
    
     public static void main(String[] args) throws Exception{
         
         HashMap <String,Aluno> lista1;
         try {
             ObjectInputStream entrada = new ObjectInputStream(new FileInputStream
            		 ("E:\\Codigos\\Java\\JavaPos\\src\\aula_io_stream\\cadAluno.bin"));
             lista1 = (HashMap <String,Aluno>) entrada.readObject();
             entrada.close();
         }catch(Exception e){
             lista1 = new HashMap<String, Aluno>();
         }
         while(true){
             System.out.println("Listagem :");
             for(Entry<String, Aluno> e1:lista1.entrySet())
                 System.out.println(e1.getValue().matricula+" :: " + e1.getValue().nome+" :: "
             +e1.getValue().telefone);
                 System.out.println("Nova Matricula ou Fim");
                 BufferedReader teclado = new BufferedReader(new InputStreamReader(System.in));
                 Aluno a1 = new Aluno();
                 a1.matricula= teclado.readLine();
                 if(a1.matricula.equalsIgnoreCase("fim"))break;
                 System.out.println("nome : ");
                 a1.nome = teclado.readLine();
                 System.out.println("telefone");
                 a1.telefone= teclado.readLine();
                 lista1.put(a1.matricula, a1);
         }
         
         ObjectOutputStream saida = new ObjectOutputStream(new FileOutputStream
        		 ("E:\\Codigos\\Java\\JavaPos\\src\\aula_io_stream\\cadAluno.bin"));
         saida.writeObject(lista1);
         saida.flush();
         saida.close();
     }
    
}
